namespace System.Xml.Serialization.Internal
{
    using System;
    using System.Globalization;

    internal static class XmlRuntimeHelper
    {
        internal static IXmlFormatAttributesInternal GetFormatAttributes(IXmlRuntimeSerializerInternal serializer)
        {
            return serializer.InternalAttributes;
        }

        internal static IXmlRuntimeSerializerInternal GetSerializer(IXmlTypeFormatter formatter)
        {
            IXmlTypeFormatterInternal _formatter = formatter as IXmlTypeFormatterInternal;
            if (_formatter != null)
            {
                IXmlRuntimeSerializerInternal _serializer = _formatter.InternalSerializer;
                if (_serializer != null)
                {
                    return _serializer;
                }
            }
            return null;
        }

        internal static IXmlRuntimeSerializerInternal GetSerializer(Type type, IXmlTypeFormatter formatter)
        {
            IXmlTypeFormatterInternal _formatter = formatter as IXmlTypeFormatterInternal;
            if (_formatter != null)
            {
                IXmlRuntimeSerializerInternal _serializer = _formatter.InternalSerializer;
                if (_serializer != null)
                {
                    if (!type.IsAssignableFrom(_serializer.RuntimeType))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Type {0} is not compatible with type {1}", type, _serializer.RuntimeType));
                    }
                    return _serializer;
                }
            }
            return null;
        }
    }
}

